package com.jztd.servone.invoice.controller;

import cn.hutool.core.util.ObjectUtil;
import com.jztd.platfrom.base.exception.ResourceNotFoundException;
import com.jztd.servone.invoice.dto.OrderDTO;
import com.jztd.servone.invoice.param.OrderParam;
import com.jztd.servone.invoice.po.BusinessConfig;
import com.jztd.servone.invoice.service.BusinessConfigService;
import com.jztd.servone.invoice.service.OrderServiceFactory;
import com.jztd.platfrom.base.annotation.UnifiedResponseBody;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单管理
 */
@UnifiedResponseBody
@RestController
@RequestMapping("/servone/order")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrderController {
    private final OrderServiceFactory orderServiceFactory;
    private final BusinessConfigService businessConfigService;

    /**
     * 查询订单信息
     */
    @PostMapping("/getOrderInfo")
    public OrderDTO getOrderInfo(@RequestBody OrderParam param) {
        param.checkData();
        BusinessConfig businessConfig = businessConfigService.getBusinessConfig(param.getClientCode());
        if (ObjectUtil.isEmpty(businessConfig)) {
            throw new ResourceNotFoundException("暂无订单信息");
        }
        return orderServiceFactory.getOrderService(businessConfig.getType()).getOrderInfo(businessConfig,param);
    }

    /**
     * 可开票列表
     */
    @PostMapping("/getInvoiceOrderList")
    public List<OrderDTO> getInvoiceOrderList(@RequestBody OrderParam param) {
        param.checkData();
        BusinessConfig businessConfig = businessConfigService.getBusinessConfig(param.getClientCode());
        if (ObjectUtil.isEmpty(businessConfig)) {
            throw new ResourceNotFoundException("暂无订单信息");
        }
        return orderServiceFactory.getOrderService(businessConfig.getType()).getInvoiceOrderList(businessConfig,param);
    }
}
